<template>
  <el-dialog class='subject-status-add-record' title="从其他项目数据库中复制" width="600px" :show-close='true'
             :visible.sync="copyVisible" :close-on-click-modal="false" :before-close="handleClose">
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="160px">
        <el-form-item label="源项目：" prop="fromProject">
          <el-select v-model="formData.fromProject" filterable placeholder="请选择" style="width: 80%" @change="handleSelectProjectChange">
            <el-option v-for="item in sourceProjectList" :key="item.id" :label="item.projectCode + item.projectName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="源项目数据库版本号：" prop="fromDatabaseId">
          <el-select v-model="formData.fromDatabaseId" placeholder="请选择" style="width: 80%" @change="handleSourceVersionChange">
            <el-option v-for="item in databaseVersionList" :key="item.id" :label="item.databaseVersion" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="源项目环境：">
          <el-radio-group v-model="formData.fromSource">
            <el-radio label="0">build</el-radio>
            <el-radio label="1" v-if="selectSourceVersion.devStatus === '1'">dev</el-radio>
            <el-radio label="2" v-if="selectSourceVersion.proStatus === '1' && selectSourceVersion.proVerNums.length > 0">pro</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据库版本号：" prop="toDatabaseVersion">
          <el-input style="width: 80%" clearable placeholder="请输入" maxlength="30" show-word-limit
                    v-model="formData.toDatabaseVersion">
          </el-input>
        </el-form-item>
        <el-form-item label="方案版本号：" prop="schemeVersion">
          <el-select v-model="formData.schemeVersion" placeholder="请选择" style="width: 80%" @change="handleSchemeVersionChange">
            <el-option v-for="item in solutionList" :key="item.id" :label="item.versionNum" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    copyVisible: Boolean,
    projectId: String,
    sourceProjectList: Array,
    solutionList: Array
  },
  
  data() {
    return {
      rules: {
        fromProject: [{ required: true, message: '请选择', trigger: 'blur' }],
        fromDatabaseId: [{ required: true, message: '请选择', trigger: 'blur' }],
        toDatabaseVersion: [{ required: true, message: '请输入', trigger: 'blur' }],
        schemeVersion: [{ required: true, message: '请选择', trigger: 'blur' }]

      },
      loading: false,
      formData: {},
      databaseVersionList: [],
      selectSourceVersion: {},
      selectSchemeVersion: {}
    }
  },

  methods: {
    ...mapActions('solutionVersion', ['copyDatabaseVersionFromOldProject', 'qryProjectDatabaseVersionNpLoading']),

    // 关闭取消
    handleClose() {
      this.selectSchemeVersion = {}
      this.selectSourceVersion = {}
      this.$refs.formRef.resetFields()
      this.$emit('handleCopyDatabaseCancel')
    },

    // 源项目change事件
    handleSelectProjectChange(value) {
      if (value) {
        const params = {
          projectId: value
        }
        this.qryProjectDatabaseVersionNpLoading({ params }).then((res) => {
          const { data, code } = res
          if (code === 200) {
            this.databaseVersionList = data || []
          }
        })
      }
    },

    // 控制源项目change事件
    handleSourceVersionChange(value) {
      this.databaseVersionList.map((dItem, dIndex) => {
        if (dItem.id === value) {
          this.selectSourceVersion = dItem

        }
      })
    },

    // 选择方案版本change事件
    handleSchemeVersionChange(value) {
      this.solutionList.map((dItem, dIndex) => {
        if (dItem.id === value) {
          this.selectSchemeVersion = dItem
        }
      })
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            ...this.formData,
            toProject: this.projectId,
            schemeVersion: this.selectSchemeVersion.versionNum,
            schemeId: this.selectSchemeVersion.id
          }
          
          this.copyDatabaseVersionFromOldProject({ data }).then(res => {
            const { code } = res
            if (code === 200) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.$refs.formRef.resetFields()
              this.selectSchemeVersion = {}
              this.selectSourceVersion = {}
              this.$emit('handleCopyDatabaseCancel', 'refresh')
            }
            this.loading = false
          })
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import './publish.scss';
</style>
